package com.example.badyloans.servlet;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.dao.TUserAccountMapper;
import com.example.badyloans.dao.TUserWalletMapper;
import com.example.badyloans.pojo.TUserAccount;
import com.example.badyloans.pojo.TUserWallet;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.badyloans.dao.TUserInfoMapper;
import com.example.badyloans.pojo.TUserInfo;
import com.example.badyloans.servlet.TUserInfoService;
@Service
public class TUserInfoServiceImpl implements TUserInfoService{

    @Resource
    private TUserInfoMapper tUserInfoMapper;
    @Resource
    private TUserInfoService tUserInfoService;
    @Resource
    private TUserWalletMapper tUserWalletMapper;
    @Resource
    private TUserAccountMapper tUserAccountMapper;

    @Override
    public int deleteByPrimaryKey(String accountId) {
        return tUserInfoMapper.deleteByPrimaryKey(accountId);
    }

    @Override
    public int insertSelective(TUserInfo record) {
        return tUserInfoMapper.insertSelective(record);
    }

    @Override
    public TUserInfo selectByPrimaryKey(String accountId) {
        return tUserInfoMapper.selectByPrimaryKey(accountId);
    }

    @Override
    public ReturnResult updateByPrimaryKeySelective(TUserInfo record) {
        TUserInfo tUserInfo1=tUserInfoMapper.selectByPrimaryKey(record.getAccountId());
        //查出来如果有数据的话就修改
        if(tUserInfo1!=null){
            int sum=tUserInfoMapper.updateByPrimaryKeySelective(record);
            if(sum>0){
                //修改成功
                return ReturnResult.succes();
            }else{
                return ReturnResult.fail("修改错误");
            }
        }else{
            TUserWallet tUserWallet=new TUserWallet();
            tUserWallet.setAccountId(record.getAccountId());
            tUserWallet.setCreditScore(100);
            tUserWallet.setCreditLine(100*100*100L);
            tUserWallet.setResidualCreditLine(100*100*100L);
            int count=tUserWalletMapper.updateByPrimaryKeySelective(tUserWallet);
            if(count>0){
                int coun=tUserInfoService.insertSelective(record);
                TUserAccount tUserAccount = new TUserAccount();
                tUserAccount.setId(record.getAccountId());
                tUserAccount.setFillUserinfo(1);
                int sum=tUserAccountMapper.updateByPrimaryKeySelective(tUserAccount);
                if(coun>0 && sum>0){
                    return ReturnResult.succes();
                }else{
                    return ReturnResult.fail("失败");
                }

            }else{
                return ReturnResult.fail("新增额度失败");
            }
        }
    }

}
